package com.mall.service.impl;

import com.mall.dao.ShoppingCartMapper;
import com.mall.entity.ShoppingCart;
import com.mall.entity.ShoppingCartVO;
import com.mall.service.ShoppingCartService;
import com.mall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    @Transactional
    public ResultVO addShoppingCart(ShoppingCart cart) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        cart.setCartTime(format);
        int i=shoppingCartMapper.insert(cart);
        if (i>0){
            return ResultVO.success();
        }
        else {
            return ResultVO.fail();
        }

    }

    @Override
    public ResultVO listShoppingCartsByUserId(Integer userId) {
        List<ShoppingCartVO> shoppingCartVOS = shoppingCartMapper.selectShopCartByUserId(userId);
        return  ResultVO.success(shoppingCartVOS);
    }

    @Override
    @Transactional
    public ResultVO updateCartNumByCartId(Integer cartId, Integer cartNum) {
        int row = shoppingCartMapper.updateCartNumByCartId(cartId, cartNum);
        if (row>0){
            return ResultVO.success();
        }
        else {
            return ResultVO.fail();
        }
    }

    @Override
    @Transactional
    public ResultVO deleteCartRecordByCartId(Integer cartId) {
        int row = shoppingCartMapper.deleteById(cartId);
        if (row>0){
            return ResultVO.success();
        }
        else {
            return ResultVO.fail();
        }

    }

    @Override
    public ResultVO listShoppingCartsByCids(Integer[] cids) {
        List<ShoppingCartVO> list = shoppingCartMapper.selectShopCartByIds(cids);
        return ResultVO.success(list);
    }
}
